package org.example.shop;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MinioConfig {
 @Value("${minio.endpoint}")
 private String endpoint;

 @Value("${minio.accessKey}")
 private String accessKey;

 @Value("${minio.secretKey}")
 private String secretKey;

 @Value("${minio.bucketName}")
 private String bucketName;

 @Bean
 public MinioClient minioClient() throws Exception {
  // 创建 MinioClient
  MinioClient minioClient = MinioClient.builder()
          .endpoint(endpoint)
          .credentials(accessKey, secretKey)
          .build();

  // 检查 bucket 是否存在，不存在则创建
  boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
  if (!found) {
   minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
  }
  return minioClient;
 }

 // Getter 方法，让其他组件能够获取到 bucketName
 public String getBucketName() {
  return bucketName;
 }
}